package tools.clustering.isodata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class isodata_analyze {

	private final static String sep="\t";
	
	public static void main(String[] args)throws Exception{
		if(args.length>0){
			if(args[0].equals("clusters")){
				if(args.length==4){
					isodata id= new isodata(args[1],true);
					id.analyzeClusters(args[2], args[3]);
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("distHist")){
				if(args.length==4){
					ObjectInputStream in=new ObjectInputStream(new FileInputStream(args[1]));
					isodata_datum datum=(isodata_datum)in.readObject();
					in.close();
					isodata_stat.distanceHistogram(datum, Double.parseDouble(args[2]), args[3]);
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("distHistT")){
				if(args.length==4){
					ObjectInputStream in=new ObjectInputStream(new FileInputStream(args[1]));
					isodata_datum datum=(isodata_datum)in.readObject();
					in.close();
					isodata_stat.distanceHistogramThreaded(datum, Double.parseDouble(args[2]), args[3]);
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("intersection")){
				if(args.length==3){
					isodata_stat.intersectionClusters(args[1]+"_many.csv", args[1]+"_intersection");
					isodata_stat.consensusClusters(args[1]+"_intersection.csv", args[1], Double.parseDouble(args[2]));
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("intersectionMax")){
				if(args.length==2){
					intersectionMax(args[1]);
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("splitIntersection")){
				if(args.length==3){
					splitIntersection(args[1], 200,args[2]);
				}else if(args.length==4){
					splitIntersection(args[1], Integer.parseInt(args[2]),args[3]);
				}else{
					System.err.println(printHelp());
					System.exit(616);
				}
			}else{
				System.err.println(printHelp());
				System.exit(616);
			}
		}else{
			System.err.println(printHelp());
			System.exit(616);
		}
		
		
		System.err.println("Done!");
		System.exit(0);
	}
	
	public static String printHelp(){
		String help="Analyzes a cluster setup:\n";
		help+="clusters <binary matrixfile> <clusterfile> <outPrefix>\n";
		help+="distHist <binary matrixfile> <binSize> <outFile>\n";
		help+="distHistT <binary matrixfile> <binSize> <outFile>\n";
		help+="intersection <prefix> <cutoff>\n";
		help+="splitIntersection <intersection file> <number of columns=200> <outPrefix>\n";
		help+="intersectionMax <intersection file>\n";
		
		return help;
	}
	
	public static void intersectionMax(String intersectionFile)throws Exception{
		BufferedReader in=new BufferedReader(new FileReader(intersectionFile));
		String s=in.readLine();
		String[] head=s.split("\t");
		int nrOfColumns=head.length;
		int max,cur;
		String[] l;
		ArrayList<String> topclusters= new ArrayList<String>();
		System.out.println("id"+sep+"max"+sep+"clusters...");
		for(s=in.readLine();s!=null;s=in.readLine()){
			l=s.split("\t");
			if(l.length==nrOfColumns){
				max=Integer.MIN_VALUE;
				for(int i=1;i<nrOfColumns;i++){
					cur=Integer.parseInt(l[i]);
					if(cur>max){
						topclusters= new ArrayList<String>();
						topclusters.add(head[i]);
						max=cur;
					}else if(cur==max){
						topclusters.add(head[i]);
					}
				}
				//print
				System.out.print(l[0]+sep+max);
				for (String cluster : topclusters) {
					System.out.print(sep+cluster);
				}
				System.out.println();
			}
		}
	}
	
	public static void splitIntersection(String intersectionFile,int N,String outPrefix)throws Exception{
		BufferedReader in=new BufferedReader(new FileReader(intersectionFile));
		String s=in.readLine();
		String[] l=s.split("\t");
		int nrOfColumns=l.length;
		int nrOfFiles=(nrOfColumns-1)/(N-1)+1;
		BufferedWriter[] out=new BufferedWriter[nrOfFiles];
		for(int i=0;i<nrOfFiles;i++){
			out[i]=new BufferedWriter(new FileWriter(outPrefix+"_sub"+i+".csv"));
		}
		for(;s!=null;s=in.readLine()){
			l=s.split("\t");
			if(l.length==nrOfColumns){
				for(int i=0;i<nrOfFiles;i++){
					out[i].write(l[0]);
					for(int j=0;j<N&&1+(N-1)*i+j<l.length;j++){
						out[i].write("\t"+l[1+(N-1)*i+j]);
					}
					out[i].write("\n");
				}
			}
		}
		for(int i=0;i<nrOfFiles;i++){
			out[i].close();
		}
	}
}
